import { request,BASEURL,requestPayment} from '../../utils/request';
Page({
  data: {
    BASEURL:'',
    id:0,
    num:1,
    addressInfo:[],
    address:{},
    goodsList:{},
    identifier:false,
    price1:''
  },
  //跳转到收货地址
  toaddress(){
    wx.navigateTo({
      url: '/pages/address/address',
    })
  },
  //获取商品详情
  async shopDetail(){
     let {openid} = wx.getStorageSync('uid')
     let {id} = this.data
      let res = await request({
      url:'/api/Wxapp/handelOrder',
      method:"POST",
      data:{
        openid,
        id
      }
    })
    let goodsList = res.data.data.product
    let address = res.data.data.address
    this.setData({
      goodsList,
      address
    })
    // 判断地址如果是空不存储 后面发送请求
    // var arr = Object.keys(address);
    if(address==null){
      console.log("111")
      this.setData({
        identifier:false
      })
      wx.setStorageSync('address',address)
    }else{
      this.setData({
        identifier:true
      })
      // 存储请求回来的地址 修改地址时有默认值
      let address = {
      ...this.data.address,
      identifier:this.data.identifier
      }
      console.log("222")
      wx.setStorageSync('address',address)
    }
  },
  // 立即支付
  async onBridgeReady(){
    var that = this
      //判断是否登录
    var uid = wx.getStorageSync('uid')
    if(!uid){
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return
    }
    // 获取收货地址
    let address = wx.getStorageSync('address')
    if(address==null){
      wx.showToast({
        title: '请选择收货地址',
        icon:"none"
      })
      return
    }
    //获取地址数据中的id
    let {id} = this.data.address
    let {openid} = wx.getStorageSync('uid')
    //提交订单
    let dd = await request({
      url:'/api/wxapp/createOrder',
      method:"POST",
      data:{
        openid,
        aid:id,
        gid:that.data.id,
        num:that.data.num
      }
    })
    console.log(dd)

    let dd1 = dd.data.data

    let res = await request({
      url:'/api/Wx_pay/wxpay',
      method:"POST",
      data:{
        oid:dd1,
        openid
      }
    })
    console.log(res)
    let pay= res.data.data
    wx.requestPayment({
      ...pay,
      success:()=>{
        wx.showToast({
          title: '支付成功',
        })
        setTimeout(()=>{
          wx.switchTab({
            url: '/pages/index/index',
          })
        },1500)
      },
      fail:()=>{
        wx.showToast({
          title: '支付失败',
        })
      }
    })
    // // 判断后台返回状态
    // if(res.data.code==0){
    //   wx.showToast({
    //     title: '仅限一单'
    //   })
    // }
  },
  onLoad: function (options) {
    var {price,num,id} = options
   let  price1 = price * num
    this.setData({
      price1,
      id,
      num,
      BASEURL
    })

    
  },

  onShow: function () {
    //获取商品详情
    this.shopDetail()
    // //收货地址
    // this.address()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let {openid} = wx.getStorageSync('uid')
    return {
      title: "邀请好友",
      path:`/pages/index/index?pid=${openid}` 
    }
  }
})